<?php

namespace app\api\controller;

use think\Controller;

class Wx extends Controller
{
    // 发送模板消息
    public function send_template_message($openid,$template_id,$data,$url = "")
    {
//        $data = array(
//            'thing4' => array('value' => "王三", 'color' => ''),
//            'amount6' => array('value' => "10000", 'color' => ''),
//            'time3' => array('value' => "2016-8-26", 'color' => ''),
//            'amount1' => array('value' => '9950', 'color' => '')
//        );
//        $openid = 'opFhH6lakZrBPLz4o1B1evlSLoyg';
//        $url = "http://community.fdkj.top";
        $topcolor = "";


        $access_token = $this->getAccessToken();
        $template = $this->setTemplate($openid, $template_id, $data, $url, $topcolor);
        $urls = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $res = $this->http_request($urls, json_encode($template));
        dump($res);
    }

    public function setTemplate($openid, $template_id, $data, $url, $topcolor)
    {
        switch ($template_id) {
            case 1:
                $tpl = "93J-JhCRM4AzCm8NiN4-fgib9ltqcEqUMH8D1EmjPbI";//商户结算到账通知
                break;
            case 2:
                $tpl = "QYesuW8SEK1pTA6uHVgcpkDbBex0o34tz419D_mau5Y";//下单成功通知 
                break;
            case 3:
                $tpl = "RfFRMk9c2yos9y2YIvFxGFeF91DPVi-VuT4C59skPns";//支付成功通知 
                break;
            case 4:
                $tpl = "xXjjLfFkMkkYH_TvLAAdGTOSmCeEDu6ayabUrurR4RM";//货物签收通知 
                break;
            case 5:
                $tpl = "xJquxj6xF2BwGFc_siiLqpTLYsI0GTC-EvExYut_Biw";//客户关联成功通知 
                break;
            case 6:
                $tpl = "WhQkvV3wIYJqYoUv5XgyaEhgWTBywU81-rXZXtaDJkk";//客户需求处理通知 
                break;
            default:
                $tpl = '';
                break;

        }
        if (!$tpl) {
            return '';
        }
        $template = array(
            'touser' => $openid,
            'template_id' => $tpl,
            'url' => $url,
            'topcolor' => $topcolor,
            'data' => $data
        );
        return $template;
    }

    // 获取 access_token
    public function getAccessToken()
    {
        $wechat = config("wechat");
        $access_token = '';
        if (session('access_token_bd')) {
            $access_token = session('access_token_bd');
        } else {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $wechat["AppID"] . "&secret=" . $wechat["AppSecret"];
            $returnData = $this->http_request($url);
            $access_token = $returnData["access_token"];
            session_write_close();
            session(['name' => 'access_token_bd', 'value' => $access_token, 'expire' => 7200]);
        }
        return $access_token;
    }

    protected function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }

    public function getWechatScan()
    {
        $url = $this->request->post("url");
        $wechat = config("wechat");
        //获取accesstoken
        $token = get_accesstoken($wechat["AppID"], $wechat["AppSecret"]);
        //获取jsapi_ticket
        $ticket = get_jsapi_ticket($token);

        //整合参数
        $sign_data['noncestr'] = createNoncestr();
        $sign_data['jsapi_ticket'] = $ticket;
        $sign_data['timestamp'] = time();
        $sign_data['url'] = $url;

        //签名
        $signature = getSign($sign_data);

        $scancon['signature'] = $signature;
        $scancon['noncestr'] = $sign_data['noncestr'];
        $scancon['timestamp'] = $sign_data['timestamp'];
        $scancon['appid'] = $wechat["AppID"];
        $scancon['jsApiList'] = ["qrCode", "barCode", "scanQRCode"];

        $this->success("请求成功", $scancon);
    }
}